package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> findByPage(Integer pageNum, Integer pageSize, String roleName) {
        PageHelper.startPage(pageNum,pageSize);
        List<RoleVo> pageResponse = roleMapper.selectByCondition(roleName);
        Page<RoleVo> page = (Page<RoleVo>)pageResponse;
        PageResponse<RoleVo> voPageResponse = PageResponse.of(page, RoleVo.class);
        return voPageResponse;
    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public Set<String> findCheckedResources(Long roleId) {
        return roleResourceMapper.selectResourcesRoleById(roleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(RoleDto dto) {
//        修改角色信息(角色名字)
        if (StringUtils.isNotBlank(dto.getRoleName())) {
            Role role = new Role();
            role.setRoleName(dto.getRoleName());
            role.setId(dto.getId());
            role.setDataState(dto.getDataState());
            roleMapper.updateByPrimaryKey(role);
        } else {
//        根据roleid 删除中间表对应的数据
            roleResourceMapper.deleteByRoleId(dto.getId());
//        把新的资源id 插入到中间表
            String[] arr = dto.getCheckedResourceNos();
            if (arr == null || arr.length == 0) {
                return;
            }
            List<RoleResource> roleResourceList = new ArrayList<>();
            for (String resourceNo : arr) {
                RoleResource roleResource = new RoleResource(dto.getId(), resourceNo, "0");
                roleResourceList.add(roleResource);
            }
            roleResourceMapper.batchInsert(roleResourceList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(Long roleId) {
//        删除中间表数据
        roleResourceMapper.deleteByRoleId(roleId);
//        删除角色表
        roleMapper.deleteByPrimaryKey(roleId);
    }

    /**
     *  角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> initRoles() {
        List<Role> roleList = roleMapper.initRoles();
        return BeanUtil.copyToList(roleList,RoleVo.class);
    }
}
